//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package elite.sysfunServlet.redis;

import com.fasterxml.jackson.databind.ObjectMapper;
import elite.bussiness.dyn.client.data.pubMDL.AllCacheMDLSql;
import elite.bussiness.dyn.client.data.pubSql.AllPublicSql;
import elite.bussiness.dyn2.client.Dyn2Refresh;
import elite.core.log.logCls;
import elite.core.log.logIF;
import elite.core.root.LogObj;
import elite.core.root.PublicRoot;
import elite.core.root.PublicVar;
import elite.core.root.StaticMethod;
import elite.sysfunServlet.root.StructRootJSON;
import java.util.Map;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component("jssRedisSub")
public class RedisMsg implements MessageListener {
    private static final logIF log = logCls.getMe(RedisMsg.class);
    @Autowired
    private RedisTemplate redisTemplate;

    public RedisMsg() {
    }

    public void onMessage(Message message, byte[] pattern) {
        byte[] body = message.getBody();
        byte[] channel = message.getChannel();

        String msg;
        try {
            msg = (String)this.redisTemplate.getValueSerializer().deserialize(body);
        } catch (Exception var10) {
            try {
                msg = new String(body);
            } catch (Exception var9) {
                log.error("消息体接收失败:" + var10.toString());
                msg = "";
            }
        }

        msg = StringEscapeUtils.unescapeJava(msg);
        log.info("==>msg：" + msg);
        // msg = StringUtils.replace(StringUtils.strip(msg, "\"\""), "\\\"", "\"");
        msg = StringUtils.strip(msg, "\"\"");
        log.info("==>msg2：" + msg);
        Map<String, Object> tt = this.JSONStrToMap(msg);
        String tmp;
        if (tt == null) {
            tt = StaticMethod.JSONStrToMap("{ \"a\":" + msg + "}", new LogObj("redismsg", ""));
            if (tt == null) {
                int ii = msg.indexOf("{");
                if (ii > -1) {
                    msg = msg.substring(ii);
                }

                tt = this.JSONStrToMap(msg);
                if (tt == null) {
                    log.error("同步消息处理失败:" + msg);
                    return;
                }
            } else {
                tmp = StaticMethod.getMapOneString(tt, "a");
                log.info("==>msg to:" + tmp);
                tt = this.JSONStrToMap(tmp);
                if (tt == null) {
                    log.error("同步消息处理失败1:" + tmp);
                    return;
                }
            }
        }

        tmp = StaticMethod.getMapOneString(tt, "t");
        String msg1 = StaticMethod.getMapOneString(tt, "d");
        log.info("==>body：" + msg + " channel:" + tmp + " msg:" + msg1);
        this.doMsg(tmp, msg1);
    }

    private Map<String, Object> JSONStrToMap(String jsonData) {
        try {
            Map<String, Object> object = (Map)(new ObjectMapper()).readValue(jsonData, Map.class);
            return object;
        } catch (Exception var3) {
            return null;
        } catch (Error var4) {
            return null;
        }
    }

    private void doMsg(String chanel, String msg) {
        log.info("asyncRedis Refresh" + chanel + " msg:" + msg);
        if ("Jss_StaffLogout".equals(chanel)) {
            PublicRoot.getInstance().getStaffManager().AsyncRedisStaffLogout(msg);
        } else if ("Jss_CacheMDLSql".equals(chanel)) {
            AllCacheMDLSql.getInstance().ClearAllSql(new LogObj("Redis Async", ""));
        } else if ("Jss_PubSql".equals(chanel)) {
            AllPublicSql.getInstance().ClearAllSql(new LogObj("Redis Async", ""));
        } else if ("Jss_Dyn2".equals(chanel)) {
            Dyn2Refresh t = new Dyn2Refresh();
            t.RedisAsync(msg);
        } else if ("Jss_Struct".equals(chanel)) {
            StructRootJSON tt = new StructRootJSON();
            tt.RedisAsync(msg);
        } else if ("formatFF".equals(chanel)) {
            LogObj logk = new LogObj("Redis  Async", "");
            Map<String, Object> tt = StaticMethod.JSONStrToMap(msg, logk);
            String mDBKey = StaticMethod.getMapOneString(tt, "db");
            PublicVar.getInstance().getFormatRoot().RefreshOneDBPool(mDBKey, new LogObj("Redis Async", ""));
        }

    }
}
